#include "taskmanager.h"
#include <QDebug>

TaskManager::TaskManager(BackupManager *backupManager, SyncManager *syncManager, QObject *parent)
    : QObject(parent), m_backupManager(backupManager), m_syncManager(syncManager), m_backupCounter(1),
    m_syncCounter(1){

    connect(m_backupManager, &BackupManager::finished, this, &TaskManager::onBackupFinished);
    connect(m_syncManager, &SyncManager::finished, this, &TaskManager::onSyncFinished);
}
void TaskManager::addBackupTask(const QString &source, const QString &target, bool incremental) {
    QMutexLocker locker(&m_mutex);
    Task task{m_backupCounter, Backup, source, target, incremental, false};
    m_tasks.append(task);
    m_backupCounter++;
    locker.unlock(); // 解锁
}

void TaskManager::addSyncTask(const QString &source, const QString &target, bool resolveConflicts) {
    QMutexLocker locker(&m_mutex);
    Task task{m_syncCounter, Sync, source, target, false, resolveConflicts};
    m_tasks.append(task);
    m_syncCounter++;
    locker.unlock(); // 解锁
}

void TaskManager::executeTask(int taskId){
    QMutexLocker locker(&m_mutex);
    if (taskId >= 0 && taskId < m_tasks.size()) {
        Task task = m_tasks.at(taskId);
        qDebug()<<"task.id:"<<task.id;

        switch (task.type) {
        case Backup:
            m_backupManager->startBackup(task.source, task.target, task.incremental);
            emit taskCompleted(QString("id:%1备份任务已完成").arg(task.id));
            // 移除已完成的任务
            removeCompletedTask(Backup);
            break;
        case Sync:
            m_syncManager->startSync(task.source, task.target, task.resolveConflicts);
            emit taskCompleted(QString("id:%1同步任务已完成").arg(task.id));
            // 移除已完成的任务
            removeCompletedTask(Sync);
            break;
        default:
            qWarning() << "Unknown task type";
            break;
        }
    } else {
        qWarning() << "Invalid task ID:" << taskId;
    }
}


void TaskManager::processTasks() {
    while (!m_tasks.isEmpty()) {
        // QMutexLocker locker(&m_mutex);
        if (m_tasks.isEmpty()) {
            return;
        }
        Task task = m_tasks.takeFirst();
        qDebug()<<"taskid:"<<task.id;
        switch (task.type) {
        case Backup:
            m_backupManager->startBackup(task.source, task.target, task.incremental);
            emit taskCompleted(QString("id:%1备份任务已完成").arg(task.id));
            break;
        case Sync:
            m_syncManager->startSync(task.source, task.target, task.resolveConflicts);
            emit taskCompleted(QString("id:%1同步任务已完成").arg(task.id));
            break;
        default:
            qWarning() << "Unknown task type";
            break;
        }
    }
}

void TaskManager::startTasks() {
    processTasks();

}

void TaskManager::removeCompletedTask(TaskType type) {
    QList<Task>::iterator it = std::remove_if(m_tasks.begin(), m_tasks.end(), [type](const Task &t) {
        return t.type == type;
    });

    if (it != m_tasks.end()) {
        m_tasks.erase(it);
    }
}

void TaskManager::onBackupFinished() {
    emit taskCompleted("备份任务已完成");

    // 获取当前任务列表中的最后一个备份任务
    QMutexLocker locker(&m_mutex);
    int lastBackupId = -1;
    for (const Task& task : m_tasks) {
        if (task.type == Backup) {
            lastBackupId = task.id;
        }
    }

    emit taskCompleted(QString("id:%1备份任务已完成").arg(lastBackupId));
    removeCompletedTask(Backup);
}

void TaskManager::onSyncFinished() {
    emit taskCompleted("同步任务已完成");

    // 获取当前任务列表中的最后一个同步任务
    QMutexLocker locker(&m_mutex);
    int lastSyncId = -1;
    for (const Task& task : m_tasks) {
        if (task.type == Sync) {
            lastSyncId = task.id;
        }
    }

    emit taskCompleted(QString("id:%1同步任务已完成").arg(lastSyncId));
    removeCompletedTask(Sync);
}

void TaskManager::startNextTask() {
    QMutexLocker locker(&m_mutex);

    locker.unlock(); // 解锁
    processTasks();
}




